package com.mscsoft.brainpower.server.service;

import javax.annotation.Resource;

import com.mscsoft.brainpower.server.dao.*;
import com.mscsoft.brainpower.server.domain.*;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class GameService {
    @Resource
    GameDao gameDao;

    public Msg<GameScore> getBestScore(int uid, int msid) {
        return new Msg<>(gameDao.queryGameScoreByUidAndMsid(uid, msid));
    }

    public Msg<Object> updateScore(int uid, int msid, int score, int combo, boolean ap, boolean fc) {
        if ((ap && !fc) || (score < 0 || score > 1000000) || combo < 0) {
            return new Msg<>(400, Msg.s400);
        }
        GameScore lastGameScore = gameDao.queryGameScoreByUidAndMsid(uid, msid);
        if (lastGameScore == null) {
            gameDao.insertGameScore(uid, msid, score, combo, ap, fc);
            return new Msg<>();
        } else if (lastGameScore.getScore() < score) {
            gameDao.updateGameScore(uid, msid, score, combo, ap, fc);
            return new Msg<>();
        } else {
            return new Msg<>(409, "未刷新记录");
        }
    }

    public Msg<ScoreRank[]> getScoreRank(int msid) {
        return new Msg<>(gameDao.queryScoreRankByMsid(msid));
    }
}
